package forum.dao.mysql;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class C3P0ConnectionPool implements ConnectionPool {
    private static C3P0ConnectionPool ourInstance;
    private ComboPooledDataSource cpds;

    public static C3P0ConnectionPool getInstance() {
        if (ourInstance == null) {
            try {
                ourInstance = new C3P0ConnectionPool();
            } catch (PropertyVetoException e) {
                // TODO auto-generated try-catch block
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
        return ourInstance;
    }

    private C3P0ConnectionPool() throws PropertyVetoException {

        Properties p = new Properties(System.getProperties());
        p.put("com.mchange.v2.log.MLog", "com.mchange.v2.log.FallbackMLog");
        p.put("com.mchange.v2.log.FallbackMLog.DEFAULT_CUTOFF_LEVEL", "OFF");
        System.setProperties(p);

        cpds = new ComboPooledDataSource();
        cpds.setDriverClass("com.mysql.jdbc.Driver");
        cpds.setJdbcUrl("jdbc:mysql://localhost:3306/forum");
        cpds.setUser("root");
        cpds.setPassword("variation");

        cpds.setMinPoolSize(5);
        cpds.setMaxPoolSize(20);
    }

    public Connection getConnection() throws SQLException {
        return cpds.getConnection();
    }
}
